# coding=utf-8

from string import Template
from typing import List
from parse.sdb_parser import ECU, TXGroup
from utils.payload import  VhalType

from utils.util import writeFile

CarConfigToSysProperties=Template(
"""
#include "PlatformProperty.h"
#include <android-base/properties.h>

VIRTUAL_PROPERTY(CCToSystemProperties) {
    builder.toTask()
${subscribeCC};
${initCC}
}
"""
)

subscribeCCTemplate=Template(
"""        .subscribeSignals([]() {
            auto signal=SIGNAL.get${signalName}();
            auto val = std::to_string(static_cast<int32_t>(signal));                
            android::base::SetProperty("vhal.${signalName}", val);
        }, SignalRX::${signalName})
"""
)

initCCTemplate=Template(
"""
    auto ${signalName}=SIGNAL.get${signalName}();
    android::base::SetProperty("vhal.${signalName}", std::to_string(static_cast<int32_t>(${signalName})));
"""
)




def gen_CCToSystemPropertiesImpl(carconfig:ECU,ccThousand:ECU):
        subscribeCCList=[]
        initCCList=[]
        for signal in carconfig.rxPackage.signals:
            subscribeCCList.append(subscribeCCTemplate.substitute(
                signalName = signal.name
            ))
            initCCList.append(initCCTemplate.substitute(
                signalName = signal.name
            ))
        
        for signal in ccThousand.rxPackage.signals:
            subscribeCCList.append(subscribeCCTemplate.substitute(
                signalName = signal.name
            ))
            initCCList.append(initCCTemplate.substitute(
                signalName = signal.name
            ))

        writeFile("../vhal_platform/common/virtual", "CCToSystemProperties.cpp",
                    lambda file: file.write(CarConfigToSysProperties.substitute(
                        subscribeCC="".join(subscribeCCList),
                        initCC = "".join(initCCList)
                    )))
    
           
                

    